package com.xiangty.reader;

import com.xiangty.bean.CustTemp;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.PathResource;
import org.springframework.validation.BindException;

/**
 * 读取文件配置类
 */
@Configuration
public class FlatFileItemReaderConfiguration {

    /**
     * 读文件
     * @return
     */
    @Bean("flatFileItemReader")
    @StepScope
    public FlatFileItemReader<CustTemp> flatFileItemReader(){
        System.out.println("------------FlatFileItemReader 开始 ");
        FlatFileItemReader<CustTemp> reader = new FlatFileItemReader<>();
        reader.setResource(new PathResource("D:\\springbatchdemo\\data.txt"));

        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
//        tokenizer.setNames(new String[]{""});
        DefaultLineMapper mapper = new DefaultLineMapper();
        mapper.setLineTokenizer(tokenizer);
        mapper.setFieldSetMapper(new FieldSetMapper() {
            @Override
            public Object mapFieldSet(FieldSet fieldSet) throws BindException {
                CustTemp custTemp = new CustTemp();
                custTemp.setCustNo(fieldSet.readString(0));
                custTemp.setApplicationNo(fieldSet.readString(1));
                custTemp.setValue1(fieldSet.readString(2));
                custTemp.setValue2(fieldSet.readString(3));
                custTemp.setValue3(fieldSet.readString(4));
                custTemp.setValue4(fieldSet.readString(5));
                custTemp.setValue5(fieldSet.readString(6));
                custTemp.setValue6(fieldSet.readString(7));
                custTemp.setValue7(fieldSet.readString(8));
                custTemp.setValue8(fieldSet.readString(9));
                custTemp.setValue9(fieldSet.readString(10));
                custTemp.setValue10(fieldSet.readString(11));
                custTemp.setValue11(fieldSet.readString(12));
                custTemp.setValue12(fieldSet.readString(13));
                custTemp.setValue13(fieldSet.readString(14));
                custTemp.setValue14(fieldSet.readString(15));
                custTemp.setValue15(fieldSet.readString(16));
                custTemp.setValue16(fieldSet.readString(17));
                custTemp.setValue17(fieldSet.readString(18));
                custTemp.setValue18(fieldSet.readString(19));
                custTemp.setValue19(fieldSet.readString(20));
                custTemp.setValue20(fieldSet.readString(21));
                return custTemp;
            }
        });

        mapper.afterPropertiesSet();
        reader.setLineMapper(mapper);
        return reader;
    }

}
